#include <QtGui/QTextEdit>
#include <QtGui/QPushButton>
#include <QtCore/QVariant>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QtDebug>

#include "reginfodialog.h"
#include "type.h"

RegInfoDialog::RegInfoDialog(QWidget *parent) :
    QDialog(parent)
{
    createItems();
    setWindowTitle(tr("登记信息"));
    this->setFixedSize (QSize(420, 450));
}
RegInfoDialog::~RegInfoDialog()
{
}
void RegInfoDialog::createItems()
{
    regInfoText = new QTextEdit(this);
    regInfoText->setGeometry(QRect(0, 5, 420, 410));
    regInfoText->setReadOnly(true);
    readInfo(regInfoText);
    okPushButton = new QPushButton(tr("确定"), this);
    okPushButton->setGeometry(QRect(160, 425, 100, 24));
    connect(okPushButton, SIGNAL(clicked()), this, SLOT(close()));
}

void RegInfoDialog::readInfo( QTextEdit * textEdit)
{
    int host = 1;
    int iobr = 0;
    int loop = 0;
    int node = 1;
    int registerNodenum = 0;
    char iobrinfo[5];
    char registerNodenuminfo[5];
    memset(iobrinfo, 0, 5);
    memset(registerNodenuminfo, 0 ,5);
    QString text;

    QSqlDatabase::database().transaction();
    QSqlQuery query;
    query.setForwardOnly(true);
    query.exec(QString("select * from iobrinfo WHERE iobrregist = 'true'"));
    while (query.next())
    {
        iobr = query.value(0).toInt();
        if(iobr <= 32)
        {
            sprintf(iobrinfo, "%02d", iobr);
            text += tr("%1板:").arg(iobrinfo);
            QSqlQuery query1;
            query1.setForwardOnly(true);
            for (loop=1;loop<=4;loop++)
            {
                registerNodenum = 0;
                query1.exec(QString("select * from unitset WHERE unitid >= %1 AND unitid <= %2").arg((host << 24) | (iobr << 16) | (loop << 8) | node)
                            .arg((host << 24) | (iobr << 16) | (loop << 8) | 0xFF));
                while(query1.next())
                {
                    if (query1.value(1).toBool() == true)
                    {
                        registerNodenum++;
                        int key = ((query1.value(7).toInt() >> 8) & 0xFF);
                        //                        qDebug()<<"key"<<key;
                        if(((query1.value(7).toInt() >> 8) & 0xFF) != 0xFF)
                        {
                            int value = devicetyep_map[key];
                            value = value + 1;
                            devicetyep_map.insert(key, value);
                            //                            qDebug()<<"devicetyep_map.contains(key)"<<devicetyep_map.contains(key);
                            //                            qDebug()<<"devicetyep_map.value(key,0)"<<devicetyep_map.value(key,0);
                            //                            devicetyep_map[key] =value + 1;
                            //                            qDebug()<<"devicetyep_map[key]"<<devicetyep_map[key];
                        }
                        //                        QMap<int,int>::iterator i = devicetyep_map.begin();
                        //                        while (i != devicetyep_map.end())
                        //                        {
                        //                            qDebug()<<"devicetyep_map.key()"<< i.key();
                        //                            qDebug()<<"devicetyep_map.key()"<< i.value();
                        //                          ++i;
                        //                        }
                    }
                }
                sprintf(registerNodenuminfo, "%03d", registerNodenum);
                text += tr("%2回路%3在线  ").arg(loop).arg(registerNodenuminfo);
            }
        }
        else if(iobr == 33)
        {
            sprintf(iobrinfo, "%02d", iobr);
            text += tr("%1板:").arg(iobrinfo);
            QSqlQuery query1;
            query1.setForwardOnly(true);
            int loop=1;

            registerNodenum = 0;
            query1.exec(QString("select * from unitset WHERE unitid >= %1 AND unitid <= %2").arg((host << 24) | (iobr << 16) | (loop << 8) | node)
                        .arg((host << 24) | (iobr << 16) | (loop << 8) | 0xFF));
            while(query1.next())
            {
                if (query1.value(1).toBool() == true)
                {
                    registerNodenum++;
                }
            }
            sprintf(registerNodenuminfo, "%03d", registerNodenum);
            text += tr("%3个楼显在线  ").arg(registerNodenuminfo);

        }
        else if(iobr == 34)
        {
            sprintf(iobrinfo, "%02d", iobr);
            text += tr("%1板:").arg(iobrinfo);
            QSqlQuery query1;
            query1.setForwardOnly(true);

            registerNodenum = 0;
            query1.exec(QString("select count(*) from zhikonginfo WHERE zhikongregist = 'true'"));
            while(query1.next())
            {
                registerNodenum =  query1.value(0).toInt() ;
            }
            if(registerNodenum > 0)
            {
                sprintf(registerNodenuminfo, "%03d", registerNodenum);
                text += tr("%3个直控盘在线  ").arg(registerNodenuminfo);
            }

            QSqlQuery query2;
            query2.setForwardOnly(true);

            query2.exec(QString("select count(*) from zongxianinfo WHERE zongxianregist = 'true'"));
            while(query2.next())
            {
                registerNodenum =  query2.value(0).toInt() ;
            }
            if(registerNodenum > 0)
            {
                sprintf(registerNodenuminfo, "%03d", registerNodenum);
                text += tr("%3个总线盘在线  ").arg(registerNodenuminfo);
            }

        }
        text += "\n";
    }
    QSqlDatabase::database().commit();
    QMap<int,int>::iterator i = devicetyep_map.begin();
    while (i != devicetyep_map.end())
    {

        qDebug()<<"devicetyep_map.key()"<< i.key();
        qDebug()<<"devicetyep_map.key()"<< i.value();
        text += Type::char_danyuan(i.key());
        text += tr("%4个在线  ").arg(i.value());
        text += "\n";
        ++i;
    }
    textEdit->setPlainText(text);
}
/*

void RegInfoDialog::readInfo( QTextEdit * textEdit)
{
    int host = 1;
    int iobr = 0;
    int loop = 0;
    int node = 1;
     int registerNodenum = 0;
    char iobrinfo[5];
    char registerNodenuminfo[5];
    memset(iobrinfo, 0, 5);
    memset(registerNodenuminfo, 0 ,5);
    QString text;

    QSqlDatabase::database().transaction();
    QSqlQuery query;
    query.setForwardOnly(true);
    query.exec(QString("select * from iobrinfo WHERE iobrregist = 'true'"));
    while (query.next())
    {
        iobr = query.value(0).toInt();
        sprintf(iobrinfo, "%02d", iobr);
        text += tr("%1板:").arg(iobrinfo);
        QSqlQuery query1;
        query1.setForwardOnly(true);
        for (loop=1;loop<=4;loop++)
        {
            registerNodenum = 0;
            query1.exec(QString("select * from unitset WHERE unitid >= %1 AND unitid <= %2").arg((host << 24) | (iobr << 16) | (loop << 8) | node)
                                            .arg((host << 24) | (iobr << 16) | (loop << 8) | 0xFF));
            while(query1.next())
            {
                if (query1.value(1).toBool() == true)
                {
                    registerNodenum++;
                }
            }
            sprintf(registerNodenuminfo, "%03d", registerNodenum);
            text += tr("%2回路%3在线  ").arg(loop).arg(registerNodenuminfo);
        }
        text += "\n";
    }
    QSqlDatabase::database().commit();
    textEdit->setPlainText(text);
}
*/
